package com.springboot.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.news.domain.dto.FruitDTO;
import com.springboot.news.domain.query.FruitQuery;
import com.springboot.news.domain.vo.FruitVO;
import com.springboot.news.domain.vo.PageableVO;
import com.springboot.news.mapper.FruitMapper;
import com.springboot.news.domain.po.Fruit;
import com.springboot.news.service.FruitService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class FruitServiceImpl extends ServiceImpl<FruitMapper, Fruit> implements FruitService {
    @Resource
    private FruitMapper fruitMapper;

    @Override
    public PageableVO<FruitVO> tableList(FruitQuery dto) {
        QueryWrapper<Fruit> queryWrapper = new QueryWrapper<Fruit>();
        queryWrapper.like("fruit_name", dto.getFruitName());
        return PageableVO.fromPageRaw(page(dto.toPage(), queryWrapper), data -> {
            FruitVO fruitVO = new FruitVO();
            BeanUtils.copyProperties(data, fruitVO);
            return fruitVO;
        });
    }
//使用wrpper
//    @Override
//    public List<Fruit> list(Fruit fruit) {
//        QueryWrapper<Fruit> wrapper = new QueryWrapper<Fruit>().select("fruit_name").like("fruit_name", fruitName);
//        QueryWrapper<Fruit> wrapper = new QueryWrapper<Fruit>().select("fruit_name", "id", "fruit_desc", "fruit_price", "create_time", "update_time").like("fruit_name", "");
//        List<Fruit> list = fruitMapper.selectList(wrapper);
//        return list;
//    }
//    使用lambda po 实体类 数据库做映射 dto 业务传输 vo 前端视图
    @Override
    public List<Fruit> list(String fruitName) {
//        List<Fruit> list = lambdaQuery().like(fruitName != null, Fruit::getFruitName, fruitName).list();
        return lambdaQuery().like(fruitName != null, Fruit::getFruitName, fruitName).list();
    }
    @Override
    public void addList(FruitDTO dto) {
        dto.setUpdateTime(LocalDateTime.now());
        dto.setCreateTime(LocalDateTime.now());
        Fruit fruit = new Fruit();
        BeanUtils.copyProperties(dto, fruit);
        save(fruit);
    }

    @Override
    public void delete(Integer id) {
        fruitMapper.deleteById(id);
    }
    @Override
    public void updateTable(FruitDTO dto) {
        dto.setUpdateTime(LocalDateTime.now());
        dto.setCreateTime(LocalDateTime.now());
//        fruitMapper.update(fruit);
//        lambdaUpdate().set(Fruit::getId, dto.getId()).set(Fruit::getFruitName, dto.getFruitName()).set(Fruit::getFruitDesc, dto.getFruitDesc()).set(Fruit::getFruitPrice, dto.getFruitPrice())
//                .set(Fruit::getCreateTime, LocalDateTime.now()).set(Fruit::getUpdateTime, LocalDateTime.now()).update();
        Fruit fruit = new Fruit();
        BeanUtils.copyProperties(dto, fruit);
        updateById(fruit);
    }

    @Override
    public FruitVO fruitDetail(Integer id) {
         Fruit fruit = getById(id);
         FruitVO vo = new FruitVO();
         BeanUtils.copyProperties(fruit, vo);
        return vo;
    }
}
